package io.renren.modules.secretary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.exception.RRException;
import io.renren.modules.common.entity.CensusEntity;
import io.renren.modules.generator.entity.ClassEntity;
import io.renren.modules.generator.service.ClassService;
import io.renren.modules.headmaster.dao.MasterCensusDao;
import io.renren.modules.secretary.dao.SecretaryCensusDao;
import io.renren.modules.secretary.service.SecretaryCensusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author wks
 * @date 2022/2/7 16:40
 */
@Service
public class SecretaryCensusServiceImpl implements SecretaryCensusService {

    @Autowired
    SecretaryCensusDao secretaryCensusDao;

    @Autowired
    ClassService classService;

    @Autowired
    MasterCensusDao masterCensusDao;

    @Override
    public Integer studentNum() {
        return secretaryCensusDao.studentNum();
    }

    @Override
    public Integer punchStudentNum(Date date) {
        return secretaryCensusDao.punchStudentNum(date);
    }

    @Override
    public CensusEntity sectorList(Date date) {
        if (Objects.isNull(date)) {
            date = new Date(System.currentTimeMillis());
        }
        CensusEntity censusEntity = new CensusEntity();
        //当前日期打卡人数
        Integer punchStudentNum = punchStudentNum(date);
        //学生总人数
        Integer studentNum = studentNum();
        //当前日期未打卡人数
        int noPunchStudentNum = studentNum - punchStudentNum;

        censusEntity.setNoPunchStudent(noPunchStudentNum);

        censusEntity.setPunchStudent(punchStudentNum);

        return censusEntity;
    }

    @Override
    public List<CensusEntity> rectangleList(Date date) {
        if (Objects.isNull(date)) {
            date = new Date(System.currentTimeMillis());
        }
        List<CensusEntity> censusEntities = null;
        QueryWrapper<ClassEntity> wrapper = new QueryWrapper<>();
        wrapper.select("class_id", "class_name");
        List<ClassEntity> classEntities = classService.list(wrapper);
        if (!classEntities.isEmpty()) {
            censusEntities = new ArrayList<>();
            for (ClassEntity classEntity : classEntities) {
                CensusEntity censusEntity = new CensusEntity();
                Long classId = classEntity.getClassId();
                Integer studentNum = masterCensusDao.studentNum(classId);
                Integer punchStudentNum = masterCensusDao.punchStudentNum(date, classId);
                int noPunchStudentNum = studentNum - punchStudentNum;
                censusEntity.setTopic(classEntity.getClassName());
                censusEntity.setPunchStudent(punchStudentNum);
                censusEntity.setNoPunchStudent(noPunchStudentNum);
                censusEntities.add(censusEntity);
            }
        } else {
            throw new RRException("暂无班级信息,无法生成统计图");
        }

        return censusEntities;
    }

}
